$(document).ready(function() {
	//回车模糊查询
	searchEvent();
	//下拉框查询
	changeEvent();
	//初始化数据
	init(1);
});
	
//默认加载  
function init(pageNo){
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/sellerWithdraw/jpa/page?pageNum="+pageNo+"&pageSize="+pageSize+"&search="+$('#search').val()+"&userType="+$('#userType').val(),
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){							
					var id = n.id;
					var status = statusStr(n.status);
					var withdrawType = withdrawTypeStr(n.withdrawType);
					var thislist = 
					'	<tr>'+
					'		<td>'+(i+1)+'</td>'+					
					'		<td>'+n.user.realName+'</td>'+				
					'		<td>'+n.user.secondId+'</td>'+
					'		<td>'+n.drawPrice+'</td>'+
					'		<td>'+n.commissionPrice+'</td>'+
					'		<td>'+n.withdrawPoundage+'</td>'+
					'		<td>'+n.sellerPrice+'</td>'+
					'		<td>'+withdrawType+'</td>'+	
					'		<td>'+status+'</td>'+
					'		<td>';
					if(n.status == 1){
						thislist +=						
							'<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="pass('+id+')"> 审核通过</a>'+
							'<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="fail('+id+')"> 审核失败</a>';
					}
					thislist +=
					'		</td>'+
					'	</tr>';
					$('#thislist').append(thislist);										
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 

				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});

}

/**
 * 审核通过
 */
function pass(id){
		$.ajax({
	        url: urlcore + "/api/sellerWithdraw/setPass?id="+id,
	        type: "get",
	        dataType: 'json',
	        async: false,
	        contentType: "application/json;charset=utf-8",
	        success:function(data){
	            if (data.success == true) {	           	
	            	location.reload();
	            }else if(data.code=='FAIL'){
	            	alert(data.msg);
	            }else if (data.code == 'OVERTIME'){
	                    var thisUrl = window.location.href;
	                    if (thisUrl.indexOf('login.html') <= -1) {
	                        top.window.location.href="login.html";
	                    }
	                } else {
	                    alert(data.msg);
	                }
	        },
	        error:function() {
	            /* Act on the event */
	            alert("error");
	        }
	    });
	    

}


/**
 * 审核失败
 */
function fail(id){
	$.ajax({
        url: urlcore + "/api/sellerWithdraw/setFail?id="+id,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	location.reload();
            } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}



//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}

/**
 * 模糊查询
 */
function searchEvent(){
	$('#search').bind('keypress',function(event){                 
         if(event.keyCode == 13) {  
              init(1);
         }  
	});
}

/**
 * 下拉框查询
 */
function changeEvent(){
       $("#userType").change(function () {
       		init(1);
   		});
}


/**
 * 用户状态返回
 * @param {Object} status
 */
function statusStr(status){
	if(status==1) {
		status = '审核中';
	}
	else if(status==2) {
		status = '审核通过';
	}	else if(status==3) {
		status = '审核失败';
	}
	return status;
}

/**
 * 用户状态返回
 * @param {Object} withdrawType
 */
function withdrawTypeStr(withdrawType){
	if(withdrawType==1) {
		withdrawType = '余额提现';
	}
	else if(withdrawType==2) {
		withdrawType = '押金提现';
	}
	return withdrawType;
}

